'use strict';

angular.module('afasApp')
  .controller('CourseTypesCtrl', ['$scope', 'Services', '$state', '$filter', function ($scope, Services, $state, $filter) {

    console.log($filter);

    $scope.courseTypes = [];
    $scope.errors = {};

    function applyRemoteData( newData ) {
      $scope.courseTypes = newData;
    }

    function loadRemoteData() {
      Services.getItems($scope.COURSETYPE_URL)
      .then(function( results ) {
        applyRemoteData( results.data );
      });
    }

    loadRemoteData();

    $scope.addCourseType = function () {
      $scope.errors = {};
      Services.add($scope.COURSETYPE_URL, $scope.courseTypeNew)
      .then(function(){
        $scope.courseTypeNew = {};
        $state.go( '^.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

    $scope.deleteCourseType = function (courseType) {
      Services.delete($scope.COURSETYPE_URL, courseType)
      .then(function(){
        loadRemoteData();
      });
    };

  }]).controller('CourseTypeCtrl', ['$scope', 'Services', '$state', '$stateParams', '$filter', function ($scope, Services, $state, $stateParams, $filter) {

    console.log($filter);

    $scope.currentCourseType = {};
    $scope.currentID = $stateParams.id;

    Services.getItem($scope.COURSETYPE_URL, $scope.currentID)
    .then(function(results){
      $scope.currentCourseType = results.data;

    });

    $scope.updateCourseType = function () {
      // CourseTypes.updateCourseType($scope.currentCourseType, $scope.currentID);
      // $state.go( 'coursetypes.show', {id: $scope.currentID} );
      Services.update($scope.COURSETYPE_URL, $scope.currentCourseType, $scope.currentID)
      .then(function(){
        $state.go( '^.show', {id: $scope.currentID} );
      });
    };

  }]);